//=============================================================================
// TFBaseCore.
//=============================================================================
class TFBaseCore extends tfStation
	placeable;

var float LastShot;
var int baseshield;

replication
{
	reliable if ( Role == ROLE_Authority )
		baseshield, LastShot;
}

function PreBeginPlay()
{
    if(tfgame(level.game) != none)
    {
        if(tfgame(level.game).GoalScore==0)
        {
            level.game.GoalScore=9999;
            maxhealth=9999000;
        }
        else maxhealth=tfgame(level.game).GoalScore*1000;
    }
    fx=spawn(class'Taskforces.TFBaseShieldFX');
    TFBaseShieldFX(fx).basec=self;
    fx.SetBase(self);

    health=maxhealth;
    settimer(1, true);
    super.PostBeginPlay();
}

simulated function Timer()
{
    baseshield=tfgri(level.Game.GameReplicationInfo).baseshield[myteam];
    if(!tfgame(level.Game).bWaitingToStartMatch)
        tfgri(level.Game.GameReplicationInfo).AddTeamRes(float(tfgame(level.game).MinimumIncome)/60, myteam, false);
}

simulated function setinitialteam(int t)
{
    MyTeam=t;
    defenderteamindex=byte(t);
    if(myteam==1) setStaticMesh(StaticMesh'TFMedia.Buildings.tfBaseCoreBlu');
    else setStaticMesh(StaticMesh'TFMedia.Buildings.tfBaseCore');
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
    local int i;
	local rotator rRot;
    local gib chunk;
    local int num;
    local controller p;
    local playercontroller pc;
    local float tmp, factor;

    if (instigatedBy == none && DelayedDamageInstigatorController != None)
		instigatedBy = DelayedDamageInstigatorController.Pawn;

    factor=1;

    if(instigatedby!=none && instigatedby.PlayerReplicationInfo!=none && instigatedby.PlayerReplicationInfo.Team.TeamIndex==myteam && DamageType!=class'TaskForces.DamTypeGenFlash')
        factor=tfgame(level.Game).friendlyfirescale;

    if( damagetype==class'engine.fell') factor=0;
    else if((myteam==0 && DamageType==class'TaskForces.DamTypeProtR') || (myteam==1 && DamageType==class'TaskForces.DamTypeProtB'))
        return;

    if(tfgri(level.game.gamereplicationinfo).baseshield[myteam]>0)
        factor/=3*tfgri(level.game.gamereplicationinfo).baseshield[myteam];

    factor*=getSDM(damageType);
    if (DamageType==class'DamTypeNKRad') factor=0;


    tmp=damage*factor;
    damage*=factor;
    tmp-=damage;
    if(tmp<1 && frand()<tmp) tmp=1;
        damage+=tmp;

    if ( Damage > 0 )
    {
        Health -= Damage;

        if(damage>0)
	    {
		    num=fmin(damage/2.5,20);
	        if(level.bdropdetail) num/=2;

            if(frand()*40<damage) PlaySound(HitSounds[rand(3)],,256,,,0.9);

            for(i=0; i<num; i++)
		    {
                spawn(class'TaskForces.tfStationSparks',,,hitlocation,rotator(Normal(hitLocation - location)));
            }

            for ( P = Level.ControllerList; P!=None; P=P.nextController )
            {
                pc=playercontroller(p);

                if(pc!=none && p.PlayerReplicationInfo!=none && p.PlayerReplicationInfo.Team.TeamIndex==myteam)
                   pc.ReceiveLocalizedMessage(class'TaskForces.tfBaseCoreDmgMsg',damage,,,self);
            }
        }


        if(MyTeam==0) tfgri(level.game.gamereplicationinfo).team[1].score=level.game.GoalScore-health/1000;
        else tfgri(level.game.gamereplicationinfo).team[0].score=level.game.GoalScore-health/1000;

        soundpitch=96-(48*health/maxhealth);

		if(instigatedby!=none && instigatedby.playerreplicationinfo!=none)
        {
            if(instigatedby.playerreplicationinfo.team.teamindex==myteam)
            {
                instigatedby.playerreplicationinfo.score-=float(damage)/200;
                tfpri(instigatedby.playerreplicationinfo).SubSkill(-float(damage)/10);
            }
            else
            {
                instigatedby.playerreplicationinfo.score+=float(damage)/200;
                tfpri(instigatedby.playerreplicationinfo).AddSkill(float(damage)/10);
            }
        }

        if(health<=0)
        {
            spawn(class'RedeemerExplosion');
	        spawn(class'TaskForces.TFNukeFlash');

            PlaySound(sound'TFMedia.Buildings.tfBCExplo',SLOT_None,1024,,10000);

            for(i=0; i<15; i++)
		    {
			    rRot=rotRand(true);
			    chunk=spawn(chunks[rand(4)],self,,location,rrot);
			    chunk.velocity=vector(rrot)*(128+rand(512));
	    	}

            /*if(instigatedBy.playerreplicationinfo!=none)
                tfgame(level.game).checkscore(instigatedBy.playerreplicationinfo);
            tfgame(level.game).checkscore(none);*/

            for ( P = Level.ControllerList; P!=None; P=P.nextController )
            {
                if(p.PlayerReplicationInfo!=none)
                    tfgame(level.game).checkscore(p.playerreplicationinfo);
            }

            destroy();
	    }
    }
}

defaultproperties
{
     BuildingName="BaseCore"
     teamflareDS=2.500000
     ObjectiveStringSuffix=" BaseCore"
     bNotBased=True
     StaticMesh=StaticMesh'TFMedia.Buildings.tfBaseCoreBlu'
     AmbientSound=Sound'TFMedia.Buildings.tfBaseCoreAmb'
     DrawScale=0.800000
     AmbientGlow=64
     bShadowCast=True
     bStaticLighting=True
     SoundVolume=255
     SoundPitch=48
     SoundRadius=256.000000
     TransientSoundVolume=0.500000
     TransientSoundRadius=768.000000
     bCollideWorld=True
     bBlockActors=True
     bBlockPlayers=True
}
